package net.hn.hnms.biz.plan.service.impl;

import net.hn.hnms.biz.plan.mapper.StatisticMapper;
import net.hn.hnms.biz.plan.request.SalesPlanReq;
import net.hn.hnms.biz.plan.service.ISaleDataAnalysisService;
import net.hn.hnms.biz.plan.vo.ExecutionSalesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * @author 刘昊
 * @date 2024-03-07 10:04
 * <p>
 * 获取近6个月同比环比数据
 */
@Service
public class SaleDataAnalysisServiceImpl implements ISaleDataAnalysisService {
    @Autowired
    private StatisticMapper statisticMapper;

    @Override
    public List<ExecutionSalesVo> comparisonOfSalesData(SalesPlanReq req) throws IllegalAccessException {

        List<ExecutionSalesVo> resultList = new ArrayList<>(Collections.emptyList());
        List<ExecutionSalesVo> saleList = statisticMapper.SaleBarChartCount(req);
        if (CollectionUtils.isEmpty(saleList)) {
            throw new IllegalAccessException("未查到销售数据");
        }

        List<MonthData> list = getLastSixMonthsComparisonDates();

        for (MonthData monthData : list) {
            String currentMonth = monthData.currentMonth;
            String previousMonth = monthData.previousMonth;
            String yearOverYear = monthData.yearOverYear;

            BigDecimal currentSalesVolume = saleList.stream()
                    .filter(item -> currentMonth.equals(item.getSalesDate()))
                    .map(ExecutionSalesVo::getSalesVolume)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);

            BigDecimal previousSalesVolume = saleList.stream()
                    .filter(item -> previousMonth.equals(item.getSalesDate()))
                    .map(ExecutionSalesVo::getSalesVolume)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);

            BigDecimal yearOverYearSalesVolume = saleList.stream()
                    .filter(item -> yearOverYear.equals(item.getSalesDate()))
                    .map(ExecutionSalesVo::getSalesVolume)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);

            ExecutionSalesVo result = new ExecutionSalesVo();
            BigDecimal nonZeroValue = BigDecimal.valueOf(1);
            result.setMonthOnMonthData(currentSalesVolume.subtract(previousSalesVolume)
                    .divide(previousSalesVolume.compareTo(BigDecimal.ZERO) == 0? nonZeroValue :previousSalesVolume, 2, RoundingMode.CEILING));

            result.setYearOnYearData(currentSalesVolume.subtract(yearOverYearSalesVolume)
                    .divide(yearOverYearSalesVolume.compareTo(BigDecimal.ZERO) == 0? nonZeroValue :yearOverYearSalesVolume, 2, RoundingMode.CEILING));
            result.setSalesDate(currentMonth);
            result.setSalesVolume(currentSalesVolume);
            resultList.add(result);
        }
        return resultList;
    }

    public static class MonthData {
        public String currentMonth;
        public String previousMonth;
        public String yearOverYear;

        public MonthData(String currentMonth, String previousMonth, String yearOverYear) {
            this.currentMonth = currentMonth;
            this.previousMonth = previousMonth;
            this.yearOverYear = yearOverYear;
        }

        @Override
        public String toString() {
            return "Current: " + currentMonth + ", Previous: " + previousMonth + ", Year Over Year: " + yearOverYear;
        }
    }

    /**
     * 获取最近6个月的同比环比日期
     *
     * @return
     */
    public static List<MonthData> getLastSixMonthsComparisonDates() {
        List<MonthData> resultList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        for (int i = 0; i < 6; i++) {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.add(Calendar.MONTH, -i);
            String currentMonthStr = sdf.format(cal.getTime());
            cal.add(Calendar.MONTH, -1);
            String previousMonthStr = sdf.format(cal.getTime());
            cal.add(Calendar.YEAR, -1);
            cal.add(Calendar.MONTH, 1);
            if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) >= 1) {
                String yearOverYearStr = sdf.format(cal.getTime());
                resultList.add(new MonthData(currentMonthStr, previousMonthStr, yearOverYearStr));
            } else {
                cal.add(Calendar.MONTH, -1);
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                String yearOverYearStr = sdf.format(cal.getTime());
                resultList.add(new MonthData(currentMonthStr, previousMonthStr, yearOverYearStr));
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        List<MonthData> list = getLastSixMonthsComparisonDates();
        for (MonthData data : list) {
            System.out.println(data);
        }
    }
}
